<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class GoodsCateController extends Controller
{
    /*
        按照格式查询分类列表
    */
    public function getAdd()
    {
    	$cates = self::CateList();
    	// dd($cates);
        return view('admin.GoodsCate.add',['res'=>$cates]);
    }
// ===========================================================================分类添加
    public function postInsert(Request $request)
    {
    	// dd($request->all());
    	$clas = $request->only(['pid','name']);
		//拼接数据
		//判断pid是否为0
		if($clas['pid'] == 0){
			$data['path'] = '0';
		}else{
			//根据pid查询path
			$res = DB::table('goods_cate')->where('id',$clas['pid'])->first();
			$clas['path'] = $res->path.','.$clas['pid'];
			// dd($clas);
		}

    	
    	$res = DB::table('goods_cate')->insert($clas);
    	if($res){
    		return redirect('admin/goodscate/index')->with('success','分类添加成功');
    	}else{
    		return back()->with('error','分类添加失败');	
    	}

    }
// ========================================================================分类列表查询
    public function getIndex()
    {
    	$cates = self::CateList();
    	foreach ($cates as $k => $v) {
    		if($v->pid != 0){
    			$res = DB::table('goods_cate')->where('id',$v->pid)->first();
    			$cates[$k]->pid = $res->name;
    		}else{
    			$cates[$k]->pid = '顶级分类';

    		}
    	}
    	
    	return view('admin.GoodsCate.index',['class'=>$cates]);
    }
// -==============================================================================删除
 	public function getDelete($id)
 	{
 		//获取id
 		
 			
 		//检测当前分类下是否有子分类
 		$res = DB::table('goods_cate')->where('pid',$id)->count();
 		if($res > 0){
            return back()->with('error','检测到当前分类下有子分类');
        }else{
 		$res = DB::table('goods_cate')->where('id',$id)->delete();
            return back()->with('error','删除成功');
        }



 		    	}

 	// ===========================================================================修改
 	public function getEdit($id)
 	{
 		//根据用户id查询数据
 		$cates = DB::table('goods_cate')->where('id',$id)->first();
 		$cate = self::CateList();
 		// dd($cate);
 		
 		return view('admin.GoodsCate.edit',['cate'=>$cates,'cates'=>$cate]);
 	}

 	public function postUpload(Request $request)
 	{
 	
 		
 		$id = $request->id;
 	
 		// 判断是否要修改分类
 		if($request->pid != $request->xpid){
 			$res = DB::table('goods_cate')->where('pid',$id)->count();
 			if($res > 0){
 			return back()->with('error','检测到当前分类下有子分类');
 			}
 		}

 		$clas = $request->only(['pid','name','status']);
		//拼接数据
		//判断pid是否为0
		if($clas['pid'] == 0){
			$data['path'] = '0';
		}else{
			//根据pid查询path
			$res = DB::table('goods_cate')->where('id',$clas['pid'])->first();
			$clas['path'] = $res->path.','.$clas['pid'];
			// dd($clas);
		}

    	
    	$res = DB::table('goods_cate')->where('id',$id)->update($clas);
    	if($res){
    		return redirect('admin/goodscate/index')->with('success','分类修改成功');
    	}else{
    		return back()->with('error','分类修改失败');	
    	}










 		

 	}

 	static public function CateList()
 	{
 		$cates = DB::select('select *,concat(path,",",id) as paths from goods_cate order by paths');
    	
    	foreach ($cates as $k => $v) {
    		//把字符串变成数组
    		$arr = explode(',',$v->path);
    		//获取长度
    		$len = count($arr)-1;
    		$cates[$k]->name = str_repeat('|----',$len).$v->name;
    	}
    	return $cates;
 	}



}